/*
*
* ScaleUI
*
* This class implements a scale for intepreting neighbourhood shading
*
*/
class ScaleUI {
  
  // -------------------------------------
  // Attributes
  // -------------------------------------
  ControlP5 controlp5;
  PApplet parent;
  DModeManager dModeMan;
  float origX, origY, wid, hei;    // dimensions for the scale
  int divs = 30;                   // number of scale divisions
  float val;
  Textlabel maxLabel, minLabel, noDataLabel, displayTitle, modeLabel;
  String ma = "";
  String mi = "";
  
  // -------------------------------------
  // Constructor
  // -------------------------------------
  ScaleUI(ControlP5 cp5, PApplet p55, float x, float y, float w, float h) {
    
    // set attributes
    
    controlp5 = cp5;
    parent = p55;
    origX = x;
    origY = y;
    wid = w;
    hei = h;
    
    // create scale labels
    
    maxLabel = controlp5.addTextlabel("maxLabel", ma, int(origX + wid + 4), int(origY + 2));
    maxLabel.setColorValue(0);
    maxLabel.hide(); // drawn during draw loop
    minLabel = controlp5.addTextlabel("minLabel", mi, int(origX + wid + 4), int(origY + hei - 8));
    minLabel.setColorValue(0);
    minLabel.hide(); // drawn during draw loop
    minLabel.setColorBackground(0);
    noDataLabel = controlp5.addTextlabel("noDataLabel", "no data", int(origX + wid + 4), int(origY + hei + 5));
    noDataLabel.setColorValue(0);
    noDataLabel.hide(); // drawn during draw loop
    noDataLabel.setColorBackground(0);
    
    // create drawing mode labels
    displayTitle = controlp5.addTextlabel("displayTitle", "displaying:", int(origX + wid + 50), int(origY + 2));
    displayTitle.setColorValue(0);
    displayTitle.hide(); // drawn during draw loop
    displayTitle.setColorBackground(0);
    modeLabel = controlp5.addTextlabel("modeLabel", "", int(origX + wid + 50), int(origY + 15));
    modeLabel.setColorValue(0);
    modeLabel.hide(); // drawn during draw loop
    modeLabel.setColorBackground(0);
    
  } // constructor
  
  // -------------------------------------
  // Draw
  // draws scale
  // -------------------------------------
  void draw(DModeManager dmm, boolean pdf) {
    
    // retrieve current drawing mode
    DMode dMode = dmm.getDMode();
    
//    // draw "frame" rectangle; if pdf, add stroke, fill white, and narrow
//    if(pdf) {
//      fill(255);
//      stroke(150);
//      rect((origX - 4), (origY - 4), 190, (hei + 20));
//    } else {
//      noStroke();
//      fill(50, 50);
//      rect((origX - 4), (origY - 4), 225, (hei + 20));
//    }
    
    // draw "frame" rectangle, but only if not pdf mode
    if(!pdf) {
      noStroke();
      fill(50, 50);
      rect((origX - 4), (origY - 4), 225, (hei + 20));
    }
    
    color col;
    noStroke();
    
    // check to make sure drawing mode needs scale drawn
    if(!(dMode.hasMetric)) {
      // metric doesn't need scale drawn
        
      // hide scale labels if mode doesn't require a scale
      minLabel.hide();
      maxLabel.hide();
      noDataLabel.hide();
      
      // show display title (ie label that says "displaying")
      displayTitle.show();
      
      // refresh mode label
      modeLabel.setValue(dMode.name);
      modeLabel.show();
      
    } else {
      
      // draw scale
      
      // pdf drawing mode has slightly different position and number of scale divisions
      if(pdf) {
        for(int i = 0; i < 10; i++) {
          
          // draw coloured portion
          val = dMode.getMax() - (i + 0.5) * (dMode.getMax() - dMode.getMin()) / 10;
          col = dMode.getColour(val);
          fill(col);
          rect(origX, (origY + 10 + hei / 10 * i), wid, (hei / 10));
          
          // draw "no data" square
          fill(noDataFil);
          rect(origX, (origY + 10 + hei + 5), wid, 8);
          
        } // for      
      } else {
        for(int i = 0; i < divs; i++) {
          
          // draw coloured portion
          val = dMode.getMax() - (i + 0.5) * (dMode.getMax() - dMode.getMin()) / divs;
          col = dMode.getColour(val);
          fill(col);
          rect(origX, (origY + hei / divs * i), wid, (hei / divs));
          
          // draw "no data" square
          fill(noDataFil);
          rect(origX, (origY + hei + 5), wid, 8);
          
        } // for
      } // if
      
      
      // draw labels
      
      // update values
      mi = nf(dMode.getMin(), 3, 1);
      ma = nf(dMode.getMax(), 3, 1);
      minLabel.setValue(mi);
      maxLabel.setValue(ma);
      modeLabel.setValue(dMode.name);
      
      // write labels using Processing font functions if writing to pdf; show P5 labels if not
      if(pdf) {
        fill(130);
        textFont(pdfExportFontSmall);
        text(ma, int(origX + wid + 4), int(origY + 18));
        text(mi, int(origX + wid + 4), int(origY + hei + 9));
        text("no data", int(origX + wid + 4), int(origY + hei + 23));
        text("displaying: " + dMode.name, int(origX), int(origY + 4));
      } else {
        // show labels
        minLabel.show();
        maxLabel.show();
        noDataLabel.show();
        displayTitle.show();
        modeLabel.show();
      }
    } // if
  } // draw method
  
} // Scale UI class
